import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		userId:0,
		userName: "",
		newMsg:0,
		lectorid:0,
		authToken:"",
	},
	mutations: {
		readmsg(state,isclear=false){
			if(isclear){
				state.newMsg = 0;
			}else{
				state.newMsg -= 1;
			}
		},
		setnewmsg(state,news=0){
			state.newMsg = news;
		},
		/**
		 * 
		 */
		setlector(state,lid=0){
			state.lectorid = lid;
		},
		/**登录成功后调用该方法传值
		 * @param {Object} state
		 * @param {Object} data
		 */
		login(state, data) {
			state.userName = data.cloudLogin.nickname;
			state.userId = data.cloudLogin.uid;
			state.lectorid = data.cloudLogin.lid;
			console.log(state)
			state.authToken = data.token;
			state.hasLogin = true;
		},
		logout(state) {
			state.userName = "";
			state.authToken = "";
			state.userId = 0;
			state.lectorid = 0;
			state.hasLogin = false;
		}
	}
})

export default store
